﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class EmployeeBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IEmployeeDA employeeDA = DataAccess.CreateEmployeeDA();
        private PubParasInfo pubParasInfo;

        public EmployeeBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public EmployeeBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> employeeIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<EmployeeInfo> employeeListInfo = new List<EmployeeInfo>();
                EmployeeInfo employeeInfo = null;
                for (i = 0; i < employeeIDS.Count; i++)
                {
                    employeeInfo = employeeDA.GetEmployeeByID(dataBase, tran, employeeIDS[i], this.dataBaseID[0], this.pubParasInfo);
                    employeeListInfo.Add(employeeInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (employeeDA.DeleteEmployee(dataBase, tran, employeeIDS, this.dataBaseID) > 0)
                {
                    for (i = 0; i < employeeIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<EmployeeInfo>(dataBase, tran, employeeListInfo[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "EMPLOYEE", this.dataBaseID[0]);
                        temp = temp + employeeIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return employeeDA.GetAllEmployee(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string employeeID)
        {
            return employeeDA.GetEmployeeByID(employeeID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            EmployeeQueryEntity employeeQuery = obj as EmployeeQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(employeeQuery.IsActive))
            {
                sqlWhere.Add(" E.IS_ACTIVE " + employeeQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(employeeQuery.EmployeeID))
            {
                sqlWhere.Add(" E.EMPLOYEE_ID " + employeeQuery.EmployeeID);
            }
            if (!string.IsNullOrEmpty(employeeQuery.EmployeeName))
            {
                sqlWhere.Add(" E.EMPLOYEE_NAME " + employeeQuery.EmployeeName);
            }
            if (!string.IsNullOrEmpty(employeeQuery.Sex))
            {
                sqlWhere.Add(" E.SEX " + employeeQuery.Sex);
            }
            if (!string.IsNullOrEmpty(employeeQuery.Position))
            {
                sqlWhere.Add(" E.POSITION " + employeeQuery.Position);
            }
            if (!string.IsNullOrEmpty(employeeQuery.DepartmentID))
            {
                sqlWhere.Add(" E.DEPARTMENT_ID " + employeeQuery.DepartmentID);
            }
            if (!string.IsNullOrEmpty(employeeQuery.MobilePhone))
            {
                sqlWhere.Add(" E.MOBILE_PHONE " + employeeQuery.MobilePhone);
            }
            if (!string.IsNullOrEmpty(employeeQuery.EntrantDate))
            {
                sqlWhere.Add(employeeQuery.EntrantDate.Replace("@EntrantDate@", " E.ENTRANT_DATE "));
            }
            if (!string.IsNullOrEmpty(employeeQuery.Remark))
            {
                sqlWhere.Add(" E.REMARK " + employeeQuery.Remark);
            }
            if (!string.IsNullOrEmpty(employeeQuery.CreatedBy))
            {
                sqlWhere.Add(" E.CREATED_BY " + employeeQuery.CreatedBy);
            }
            if (!string.IsNullOrEmpty(employeeQuery.CreatedDate))
            {
                sqlWhere.Add(employeeQuery.CreatedDate.Replace("@CreatedDate@", " E.CREATED_DATE "));
            }
            return employeeDA.GetEmployeeByQueryList(sqlWhere, employeeQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string employeeID = string.Empty;
            try
            {
                EmployeeInfo employeeInfo = obj as EmployeeInfo;
                if (employeeDA.CheckEmployeeIDUnique(employeeInfo.EmployeeID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2650_001", this.pubParasInfo.UserLanguage, new List<string> { employeeInfo.EmployeeID });
                }
                employeeInfo.CreatedDate = DateTime.Now;
                employeeInfo.UpdatedDate = DateTime.Now;
                employeeInfo.UpdatedBy = employeeInfo.CreatedBy;
                if (employeeDA.InsertEmployee(employeeInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<EmployeeInfo>(null, employeeInfo, AuditAction.Insert, this.pubParasInfo.UserID, "EMPLOYEE", this.dataBaseID[0]);
                    employeeID = employeeInfo.EmployeeID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return employeeID;
        }

        public string Update(object obj)
        {
            string employeeID = string.Empty;
            try
            {
                EmployeeInfo employeeInfo = obj as EmployeeInfo;
                employeeInfo.UpdatedDate = DateTime.Now;
                EmployeeInfo oldEmployeeInfo = employeeDA.GetEmployeeByID(employeeInfo.EmployeeID, this.dataBaseID[0], this.pubParasInfo);
                if (employeeDA.UpdateEmployee(employeeInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<EmployeeInfo>(oldEmployeeInfo, employeeInfo, AuditAction.Update, this.pubParasInfo.UserID, "EMPLOYEE", this.dataBaseID[0]);
                    employeeID = employeeInfo.EmployeeID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return employeeID;
        }
    }
}

